import * as vscode from 'vscode';
import fs from 'fs';

import fileUtils from '../utils/fileUtils';
import configUtils from '../utils/ConfigUtils';
import  tomcatHotTreeView from './TreeView';
import { MessagebyTime } from '../utils/Message';
/**
 * 单例webview
 */
class Webview {
    static webview: vscode.WebviewPanel | null = null;
    currentName : String | null = null;
    context?: vscode.ExtensionContext ;
    static select: string | null = null;
    init(context: vscode.ExtensionContext) {
        this.context = context;
    }

    getWebview(name: string, select: string|null = null) {
        if(this.context === undefined){ return; }
        if (Webview.webview === null ) {
            Webview.webview = vscode.window.createWebviewPanel("添加tomcat", "tomcat", vscode.ViewColumn.One, {
                enableScripts: true,
                localResourceRoots: [vscode.Uri.joinPath(this.context.extensionUri,'static')]
            }); 
            // 创建webview 时指定消息接受者
            Webview.webview.webview.onDidReceiveMessage(this.receive);
            let data = fs.readFileSync(vscode.Uri.joinPath(this.context.extensionUri,'static', name).fsPath, 'utf8');


            Webview.webview.webview.html = data;
            Webview.webview.onDidDispose(() => {
                Webview.webview = null;
            });
        }else{
            // 页面不相同的时候会去刷新页面
            if(this.currentName !== name || select !== Webview.select){
                let data = fs.readFileSync(vscode.Uri.joinPath(this.context.extensionUri,'static', name).fsPath, 'utf8');

                Webview.webview.webview.html = data;
            }
        }
        this.currentName = name;
        Webview.select = select;
        // 激活webview
        Webview.webview.reveal();
        Webview.webview?.webview.postMessage({type: "init", data: Webview.select});
    }

    close(){
        Webview.webview?.dispose();
    }
    // 接收到webview消息请求
    receive(message: any){
        switch(message.page){
            case "tomcat.html": {
                switch(message.type){
                    case "openFile": {
                        let path = configUtils.getConfig()["server"][message.config]['installPath']+"config/"+message.name;
                        vscode.workspace.openTextDocument(path);
                    }
                    case 'dir':{
                        fileUtils.getFileOrDirectory((path: string)=>{
                            Webview.webview?.webview.postMessage({
                                ...message,
                                path: path
                            });
                        });
                        break;
                    }
                    case "file" : {
                        fileUtils.getFileOrDirectory((path: string)=>{
                            if(path.endsWith("java") || path.endsWith("java.exe")){
                                Webview.webview?.webview.postMessage({
                                    ...message,
                                    path: path});
                            }else{
                                new MessagebyTime("错误提示: ", "选择文件不是java可执行程序", 3, 1);
                            }
                        }, true, false, {"java": ["*"] });
                        break;
                    }
                    case 'subit':{
                        let config = message['config'];
                        if(config === null || config.name === "" || config.name === undefined && config.installPath === "" || config.installPath === undefined){
                            new MessagebyTime("必填参数不能为空: ", "参数错误", 3, 1);
                            return;
                        }
                        if(config['type'] === 'change'){
                            configUtils.removeServer(config['deleteName']);
                            delete config['type'];
                            delete config['deleteName'];
                        }
                        let key = config['name'];
                        delete config.name;
                        configUtils.addServer(key, config);
                        tomcatHotTreeView.flush();
                        webView.close();
                        break;
                    }  
                    case 'close':{
                        webView.close();
                        break;
                    }
                    case 'init':{
                        Webview.webview?.webview.postMessage({type: "init", data: Webview.select});
                    }
                }
            } 
            case "develop.html": {
                switch (message.type){
                    case "init": {
                        Webview.webview?.webview.postMessage({type: "init", data: Webview.select});
                        break;
                    }
                    case "openFile": {
                        let path = configUtils.getConfig()["server"][message.config]['installPath']+"config/"+message.name;
                        vscode.workspace.openTextDocument(path);
                    }
                    case 'dir':{
                    } 
                    case 'save': {
                        let name = message.data.name;
                        delete message.data.name;
                        configUtils.addDevelop(name, message.data);
                        configUtils.flush();
                        tomcatHotTreeView.flush();
                    }   
                }
            }
        }

    }
}
const webView = new Webview();
export default webView ;